package yns.springboot.guide.demo.json;

import java.text.SimpleDateFormat;
import java.util.TimeZone;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.fasterxml.jackson.annotation.JsonInclude.Include;

import tools.jackson.databind.DeserializationFeature;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.cfg.DateTimeFeature;
import tools.jackson.databind.json.JsonMapper;

@Configuration
public class JacksonMapperConfiguration {
	
	@Bean
	public ObjectMapper initializeObjectMapper() {
	    //序列化日期格式
        //javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
        //javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer());
        //javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer());

        //反序列化日期格式
        //javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
        //javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer());
        //javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer());

		
//		ObjectMapper mapper = new ObjectMapper();
//
//		// 在反序列化时忽略在 json 中存在但 Java 对象不存在的属性
//		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//		// 在序列化时日期格式默认为 yyyy-MM-dd'T'HH:mm:ss.SSSZ
//		mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
//
//		// 在序列化时忽略值为 null 的属性
////		mapper.setSerializationInclusion(Include.NON_NULL); 
//		mapper.setSerializationInclusion(Include.CUSTOM);
//		// 忽略值为默认值的属性
//		// mapper.setDefaultPropertyInclusion(Include.ALWAYS);
//		// Java8时间序列化
//		mapper.registerModule(new JavaTimeModule());
//		// 系统默认时区
//		mapper.setTimeZone(TimeZone.getDefault());
		
		ObjectMapper mapper = JsonMapper.builder().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
				  .changeDefaultPropertyInclusion(incl -> incl.withValueInclusion(Include.CUSTOM))
				  .changeDefaultPropertyInclusion(incl -> incl.withContentInclusion(Include.ALWAYS))
				  .defaultDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ "))
				  .defaultTimeZone(TimeZone.getDefault())  //default time zone is UTC
				  .enable(DateTimeFeature.WRITE_DATES_WITH_ZONE_ID)
				  .build();
		
		return mapper;
	}


}
